package com.zhima.annotation;

import com.config.interceptor.ZhimaOjWebMvcConfiguration;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

public class ZhimaParseTokenImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableParseToken.class.getName());
        boolean letGo = (boolean) annotationAttributes.getOrDefault("value", true);
        RootBeanDefinition zhimaOjWebMvcConfigurationBeanDefinition = new RootBeanDefinition();
        zhimaOjWebMvcConfigurationBeanDefinition.setBeanClass(ZhimaOjWebMvcConfiguration.class);
        ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
        constructorArgumentValues.addIndexedArgumentValue(0, letGo);
        zhimaOjWebMvcConfigurationBeanDefinition.setConstructorArgumentValues(constructorArgumentValues);
        registry.registerBeanDefinition("zhimaOjWebMvcConfiguration", zhimaOjWebMvcConfigurationBeanDefinition);
    }
}
